package com.dou.controller;

import com.dou.mapper.DepartmentDao;
import com.dou.mapper.EmployeeDao;
import com.dou.pojo.Department;
import com.dou.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/07/2021/7/19/14:02
 * @Description:
 */
@Controller
public class employeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees =employeeDao.getALL();
        model.addAttribute("emps",employees);
        return "list";
    }
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //查询所有的部门信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "add";
    }
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        //保存员工的信息
        System.out.println(employee);
        if(employee.getDepartmentId()==101){
            employee.setDepartmentName("教学部");
        }if(employee.getDepartmentId()==102){
            employee.setDepartmentName("市场部");
        }if(employee.getDepartmentId()==103){
            employee.setDepartmentName("教研部");
        }if(employee.getDepartmentId()==104){
            employee.setDepartmentName("运营部");
        }if(employee.getDepartmentId()==105){
            employee.setDepartmentName("后勤部");
        }
        employeeDao.save(employee);
        // 回到员工列表页面，可以使用redirect或者forward，就不会被视图解析器解析
        return "redirect:/emps";
    }
    @RequestMapping("/drop")
    public String dropEmp(@RequestParam(value = "id") int id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }
    @GetMapping("/update")
    public String toUpdateEmp(@RequestParam int id,Model model){
        Employee employeeById = employeeDao.getEmployeeById(id);
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        model.addAttribute("emp",employeeById);
        return "update";
    }
    @PostMapping("/update")
    public String update(Employee employee){
        if(employee.getDepartmentId()==101){
            employee.setDepartmentName("教学部");
        }if(employee.getDepartmentId()==102){
            employee.setDepartmentName("市场部");
        }if(employee.getDepartmentId()==103){
            employee.setDepartmentName("教研部");
        }if(employee.getDepartmentId()==104){
            employee.setDepartmentName("运营部");
        }if(employee.getDepartmentId()==105){
            employee.setDepartmentName("后勤部");
        }
        employeeDao.update(employee);
        return "redirect:/emps";
    }
}
